<template>
  <CustomDialog v-model:visible="dialogVisible">
    <div class="dialog-content">
      <div class="title">全单等叫</div>
      <div class="description">
        <div class="label">等叫提醒时间</div>
        <el-time-picker
          v-model="waitTime"
          format="HH:mm"
          value-format="HH:mm:ss"
          placeholder="选择等叫时间"
          class="time-picker"
          style="width: 2rem"
        />
      </div>
      <div class="btn-box">
        <div class="cancel-btn" :class="{ 'btn-loading': loading }" @click="handleConfirm(0)">
          <span v-if="!loading">全部等叫不打印</span>
          <span v-else>处理中...</span>
        </div>
        <div class="confirm-btn" :class="{ 'btn-loading': loading }" @click="handleConfirm(1)">
          <span v-if="!loading">全部等叫并打印</span>
          <span v-else>处理中...</span>
        </div>
      </div>
    </div>
  </CustomDialog>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue'
import { ElMessage } from 'element-plus'
import CustomDialog from '@/components/CustomDialog/Index.vue'

interface Props {
  modelValue: boolean
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'confirm', payload: { printType: number; waitTime?: string }): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const waitTime = ref<string>('')
const loading = ref<boolean>(false)

// 使用computed来处理v-model
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit('update:modelValue', value)
})

// 监听弹框显示状态，重置数据
watch(
  () => props.modelValue,
  newVal => {
    if (newVal) {
      // 重置时间选择
      waitTime.value = ''
    }
  }
)

// 确认提交事件
const handleConfirm = async (printType: number) => {
  if (loading.value) return // 防止重复点击

  loading.value = true

  try {
    const payload = {
      printType,
      waitTime: waitTime.value
        ? `${new Date().toISOString().split('T')[0]} ${waitTime.value}`
        : undefined
    }

    emit('confirm', payload)
    dialogVisible.value = false
  } catch (error) {
    console.error('等叫失败:', error)
    ElMessage.error('等叫失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
  .title {
    font-size: 20px;
    font-family:
      PingFang SC,
      PingFang SC-Bold;
    font-weight: 700;
    text-align: center;
    color: #333333;
  }

  .description {
    margin-top: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 48px;
    font-size: 16px;
    font-family:
      PingFang SC,
      PingFang SC-Regular;
    font-weight: 400;
    text-align: center;
    color: #333333;

    .label {
      margin-right: 20px;
    }

    .time-picker {
      width: 200px;
    }
  }

  .btn-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 84px;
    gap: 20px;

    .cancel-btn {
      width: 200px;
      height: 60px;
      background: #ffffff;
      border: 1px solid #333333;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #333333;
      line-height: 60px;
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover:not(.btn-loading) {
        background: #f5f5f5;
      }

      &.btn-loading {
        cursor: not-allowed;
        opacity: 0.6;
        background: #f5f5f5;
      }
    }

    .confirm-btn {
      width: 200px;
      height: 60px;
      background: #7c0a03;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #ffffff;
      line-height: 60px;
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover:not(.btn-loading) {
        background: #5a0802;
      }

      &.btn-loading {
        cursor: not-allowed;
        opacity: 0.6;
        background: #999;
      }
    }
  }
}

::v-deep {
  .el-input__inner {
    border-radius: 12px;
  }
}
</style>
